package cn.zhf.shop.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.zhf.shop.config.AccessTokenService;
import cn.zhf.shop.config.GlobalConfig;


import javax.annotation.Resource;

public class WxUtils {
    @Resource
    private GlobalConfig globalConfig;

    @Resource
    private AccessTokenService accessTokenService;
    private  static String APP_ID = "wxff49e5a582930b5e";
    private  static String AppSecret = "9605a94f43cea31f3d7b51379426266b";
    public static String APP_DOMAIN ="www.oacloudapp.cn";
    public String getTicket(){
        String accessToken = accessTokenService.getAccessToken();
        String url="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+accessToken+"&type=jsapi";
        String result = sendGet(url,"utf-8",60000);
        System.out.println("Ticket:"+result);
        JSONObject json= JSONUtil.parseObj(result);
        String ticket = json.getStr("ticket");
        return ticket;
    }

    /**
     *
     * @title  sendGet
     * @Description
     * @author sss
     * @Date 2018-5-18上午11:15:33
     * @param url
     * @param charset
     * @param timeout
     * @return
     */
    public static String sendGet(String url, String charset, int timeout)
    {
        String result = "";
        try
        {
            URL u = new URL(url);
            try
            {
                URLConnection conn = u.openConnection();
                conn.connect();
                conn.setConnectTimeout(timeout);
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
                String line="";
                while ((line = in.readLine()) != null)
                {
                    result = result + line;
                }
                in.close();
            } catch (IOException e) {
                return result;
            }
        }
        catch (MalformedURLException e)
        {
            return result;
        }
        return result;
    }
}